	/*
 * jQuery Reveal Plugin 1.03
   sxwking@qq.com

*/


(function($) {

/*---------------------------
 Defaults for Reveal
----------------------------*/



/*---------------------------
 Extend and Execute
----------------------------*/
    var modal_zindex=0;

    $.fn.reveal = function(options) {


        var defaults = {
	    	animation: 'none', //fade, fadeAndPop, none
		    animationspeed: 500, //how fast animtions are
		    closeonbackgroundclick: true, //if you click background will modal close?
		    dismissmodalclass: 'close-reveal-modal', //the class of a button or element that will close an open modal
			dismissJqObject:'[action="cancel"]'
    	};

        //Extend dem' options
        var options = $.extend({}, defaults, options);


        //$(this).each(function() {

/*---------------------------
 Global Variables
----------------------------*/

        modal_zindex=modal_zindex+1;
		$(this).attr("topMeasure",50);
		$(this).attr("topOffset", $(this).height() + parseInt($(this).attr("topMeasure")));
		$(this).attr("locked",false);
		$(this).attr("animation",options.animation);
		$(this).attr("animationspeed",options.animationspeed);
		$(this).attr("closeonbackgroundclick",options.closeonbackgroundclick);
		$(this).attr("dismissmodalclass",options.dismissmodalclass);
		$(this).attr("dismissJqObject",options.dismissJqObject);
		$(this).attr("z-index",$(this).css("z-index"));
		$(this).attr("switch","open");


		$(this).css("z-index", parseInt($(this).attr("z-index"))+modal_zindex);

  		modal = $(this);
		modalBG = $('.reveal-modal-bg');



/*---------------------------
 Create Modal BG
----------------------------*/
		if(modalBG.length == 0) {
			modalBG = $('<div class="reveal-modal-bg" />').insertAfter($(this));
		}

        openModal($(this));




/*---------------------------
 Open and add Closing Listeners
----------------------------*/


			//Close Modal Listeners
			// $(this).find().one('click', function (e) {

			// 	closeModal($(e.target).parents(".reveal-modal"));
			// 	unlockModal()
			// });





			//单个关闭对象

			$($(this).attr('dismissJqObject')).bind('click', function (e) {

				closeModal($(e.target).parents(".reveal-modal"));
				//unlockModal();
			 });


			$('.' + $(this).attr("dismissmodalclass")).bind('click', function (e) {

				closeModal($(e.target).parents(".reveal-modal"));
				//unlockModal();
			 });
//			console.log($(this).find($(this).attr('dismissJqObject')+',.' + $(this).attr("dismissmodalclass")))
//




            modalBG.bind('click', function (e) {
			    if(openModalCount()<2){
					closeModal($(".reveal-modal[switch='open']"));
				// 	//unlockModal();
				}
			});
			// if($(this).attr("closeonbackgroundclick")=="true") {

			//    modalBG.click(function(){

			// 		closeModal(modal);
			// 		unlockModal();
			//    });
			//    $(this).css({"cursor":"pointer"})

			// };


			$('body').keyup(function(e) {
        		if(e.which===27){ $(this).trigger('reveal:close'); } // 27 is the keycode for the Escape key
			});




      //  });//each call



    }


	$.fn.closeModal=function(){


		var defaults = {
	    	animation: 'none', //fade, fadeAndPop, none
		    animationspeed: 300, //how fast animtions are
		    closeonbackgroundclick: true, //if you click background will modal close?
		    dismissmodalclass: 'close-reveal-modal' //the class of a button or element that will close an open modal
			//dismissJqObject:[$("#cancelbtn"),$("#okbtn")]
    	};

        //Extend dem' options
        var options = $.extend({}, defaults, options);

		$(this).attr("topMeasure",50);
		$(this).attr("topOffset", $(this).height() + $(this).attr("topMeasure"));
		$(this).attr("locked",false);
		$(this).attr("animation",options.animation);
		$(this).attr("animationspeed",options.animationspeed);
		$(this).attr("closeonbackgroundclick",options.closeonbackgroundclick);
		$(this).attr("dismissmodalclass",options.dismissmodalclass);


		closeModal($(this));

	};









	function openModal(modal){




//		console.log("o:"+openModalCount());
		//console.log(modal.attr("animation"))
		if(!modal.locked) {
			lockModal();
			if(modal.attr("animation") == "fadeAndPop") {
				modal.css({'top': $(document).scrollTop()-parseInt(modal.attr("topOffset")), 'opacity' : 0, 'display' : 'block'});
				modalBG.fadeIn(parseInt(modal.attr("animationspeed"))/2);
				modal.delay(parseInt(modal.attr("animationspeed"))/2).animate({
					"top": $(document).scrollTop()+parseInt(modal.attr("topMeasure")) + 'px',
					"opacity" : 1
				}, parseInt(modal.attr("animationspeed")),unlockModal());

			}
			if(modal.attr("animation") == "fade") {

				modal.css({'opacity' : 0, 'display' : 'block', 'top': $(document).scrollTop()+parseInt(modal.attr("topOffset")) + 'px'});

				modalBG.fadeIn(parseInt(modal.attr("animationspeed"))/2);
				modal.delay(parseInt(modal.attr("animationspeed"))/2).animate({
					"opacity" : 1
				}, parseInt(modal.attr("animationspeed")),unlockModal());
			}
			if(modal.attr("animation") == "none") {
				modal.css({'display' : 'block', 'top':$(document).scrollTop()+parseInt(modal.attr("topMeasure"))});

				modalBG.css({"display":"block"});
				unlockModal()
			}
		}

	}


	function closeModal(modal){


	    //console.log(modal.attr("animation"))
		//console.log("z:"+parseInt($(this).css("z-index")))
//		console.log("o:"+openModalCount());

	  if(!locked) {
			lockModal();
			$(modal).attr("switch","close");
			$(modal).css("z-index", parseInt($(modal).attr("z-index")));

			if(modal.attr("animation") == "fadeAndPop") {
				if(openModalCount()<1){

					modalBG.delay(parseInt(modal.attr("animationspeed"))).fadeOut(parseInt(modal.attr("animationspeed")));
				}

				modal.animate({
					"top":  $(document).scrollTop()-parseInt(modal.attr("topOffset")) + 'px',
					"opacity" : 0
				}, parseInt(modal.attr("animationspeed"))/2, function() {
					modal.css({'top':parseInt(modal.attr("topMeasure"))  + 'px', 'opacity' : 1, 'display' : 'none'});

					unlockModal();
				});



			}
			if(modal.attr("animation") == "fade") {
				if(openModalCount()<1){
					modalBG.delay(parseInt(modal.attr("animationspeed"))).fadeOut(parseInt(modal.attr("animationspeed")));
				}

				modal.animate({
					"opacity" : 0
				}, modal.animationspeed, function() {
					modal.css({'opacity' : 1, 'display' : 'none',  'top' : parseInt(modal.attr("topMeasure"))});

					unlockModal();
				});




			}
			if(modal.attr("animation") == "none") {
				if(openModalCount()<1){
					modalBG.css({'display' : 'none'});
				}

				modal.css({'display' : 'none', 'top' : parseInt(modal.attr("topMeasure"))});
		        unlockModal();


			}



		}






	}


    openModalCount = function(){

		var openModal=$(".reveal-modal[switch='open']").size();
		// $(".reveal-modal").each(function(){
		// 	 //获取已打开的弹窗数
		// 	 if($(this).css("display")!="none"){
		// 		 openModal++;
		// 	 }
		//  });
		 return  openModal;

	}



/*---------------------------
 Animations Locks
----------------------------*/
	function unlockModal() {
		locked = false;
	}
	function lockModal() {
		locked = true;
	}

})(jQuery);